<?php
require 'config.php';

$cityid = get_option_value("cityid", "101010100");
$date = date("Ymd", time());

$obj = $MEM->get($cityid);
if (empty($obj) || $obj->date != $date) {
	init_weather($cityid, $date);
}

echo print_j($MEM->get($cityid));

function init_weather($cityid, $date) {
	global $MEM;
	$obj = duba_weather($cityid, $date);
	if ($obj != null) {
		$obj->date = $date;
	}
	$MEM->set($cityid, $obj, 0, 600);
}

function duba_weather($cityid, $date) {
	$ret = new stdClass();
	$str = @file_get_contents("https://www.duba.com/static/weather_info/{$cityid}.html?v=".time());
	if (isset($str)) {
		$str = substr($str, 17, -1);
		$obj = json_decode($str);
		$ret->weatherinfo = new stdClass();
		$ret->weatherinfo->weather1 = $obj->weatherinfo->weather1;
		$img1 = $obj->weatherinfo->img1;
		if (strlen($img1) == 1) {
			$img1 = "0".$img1;
		}
		$ret->weatherinfo->img1 = "http://www.bjbsh.com/api/weather/img/d{$img1}.png";
		return $ret;
	} else {
		return null;
	}
}